<template>
  <div class="update-birthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import dayjs from 'dayjs'

export default {
  name: 'UpdateBirthday',
  components: {},
  model: {
    prop: 'userBirthday',
    event: 'update-birthday'
  },
  props: {
    userBirthday: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.userBirthday)
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  // 方法集合
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })

      const data = dayjs(this.currentDate).format('YYYY-MM-DD')

      // 请求提交更新用户性别
      await updateUserProfile({
        birthday: data
      })
      // 更新成功 => 修改父组件的 name => 关闭弹出层
      this.$emit('update-birthday', data)
      this.$emit('close')

      this.$toast.success('保存成功')
    }
  }
}
</script>

<style lang="less" scoped></style>
